package com.example.service;

import com.example.dao.StockDao;
import com.example.entity.Stock;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StockService {

    @Resource
    private StockDao stockDao;

    public void save(Stock stock) {
        stockDao.save(stock);
    }

    public void del(Long id) {
        stockDao.deleteById(id);
    }

    public Stock findById(Long id) {
        return stockDao.findById(id).orElse(null);
    }

    public List<Stock> findAll() {
        return stockDao.findAll();
    }

    public Page<Stock> findPage(Integer pageNum, Integer pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageRequest = PageRequest.of(pageNum - 1, pageSize, sort);
        return stockDao.findAll(pageRequest);
    }

    //模糊查询
    //1 根据名称查询
    public List<Stock> findLikeName(String name){
       return stockDao.findStockByName(name);
    }

    //2 根据厂家查询
    public List<Stock> findLikeFactory(String factoryName){
        return stockDao.findStockByManuFacturer(factoryName);
    }

    //3 根据供应商查询
    public List<Stock> findLikeSupplier(String supplier){
        return stockDao.findStockBySupplier(supplier);
    }

}
